package cc.ycws.core.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;

/**
 * 日期类
 *
 * @author wwl
 */
public class DataUtil {

    public String data() {
        Date data = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String newData = dateFormat.format(data);
        return newData;
    }

    public static String data5() {
        Date data = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String newData = dateFormat.format(data);
        return newData;
    }

    public static String data1() {
        Date data = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String newData = dateFormat.format(data);
        return newData;
    }

    public static String data4() {
        Date data = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        String newData = dateFormat.format(data);
        int InewData = Integer.parseInt(newData);
        int SnewData = InewData + 1;
        String rnewData = String.valueOf(SnewData);
        return rnewData;
    }

    public static String data2(int n) {
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR) + n;
        int month = cal.get(Calendar.MONTH) + 1;
        int day = cal.get(Calendar.DAY_OF_MONTH);
        int hour = cal.get(Calendar.HOUR_OF_DAY);
        int minute = cal.get(Calendar.MINUTE);
        int second = cal.get(Calendar.SECOND);
        String m;
        String d;
        String h;
        String mi;
        String s;
        if (month <= 9) {
            m = "-0" + month;
        } else {
            m = "-" + month;
        }
        if (day <= 9) {
            d = "-0" + day;
        } else {
            d = "-" + day;
        }
        if (hour <= 9) {
            h = "0" + hour;
        } else {
            h = "" + hour;
        }
        if (minute <= 9) {
            mi = "0" + minute;
        } else {
            mi = "" + minute;
        }
        if (second <= 9) {
            s = "0" + second;
        } else {
            s = "" + second;
        }
        String total = year + m + d + " " + h + ":" + mi + ":" + s;
        return total;
    }

    public static String data3(int y) {
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR) + y;
        int month = cal.get(Calendar.MONTH) + 1;
        int day = cal.get(Calendar.DAY_OF_MONTH);
        String m;
        String d;
        if (month <= 9) {
            m = "0" + month;
        } else {
            m = "" + month;
        }
        if (day <= 9) {
            d = "0" + day;
        } else {
            d = "" + day;
        }
        String total = year + m + d;
        return total;
    }

    public static int compare_date(String DATE1, String DATE2) {

        DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm");
        try {
            Date dt1 = df.parse(DATE1);
            Date dt2 = df.parse(DATE2);
            if (dt1.getTime() < dt2.getTime()) {
                return 1;
            } else if (dt1.getTime() > dt2.getTime()) {
                return -1;
            } else {
                return 0;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return 0;
    }

    public static String getRandomString(int length) {
        String base = "abcdefghijklmnopqrstuvwxyz0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }

    public static void main(String[] args) throws Exception {
		/*SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date d1 = sdf.parse(DataUtil.data5());	//有效期
		Date d2 = sdf.parse("2017-11-27");		//到期时间

		if(d1.compareTo(d2)==-1){
			System.out.println("还末到期");
		}else{
			System.out.println("到期");
		}*/

		/*String str="<html><head><title>413 Request Entity Too Large</title></head>";
		String t=str.substring(1, 5);
		String t1=str.substring(19, 22);
		System.out.println(t);*/
    }
}
